<template>
  <div class="app-container" id='Brand'>
    <div class="filter-container" style="float: right;">
      <!-- <el-input v-model="listQuery.id" clearable class="filter-item" style="width: 200px;" placeholder="请输入品牌商ID">
      </el-input> -->
      <el-input v-model="listQuery.name" clearable class="filter-item" style="width: 200px;" placeholder="请输入品牌商名称">
      </el-input>
      <el-button type="primary" class="filter-item" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <el-button type="primary" class="filter-item" icon="el-icon-edit" @click="handleCreate">新增</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%" border fit highlight-current-row>
      <el-table-column prop="id" align="center" label="品牌商ID" sortable width="120">
      </el-table-column>
      <el-table-column prop="name" align="center" label="品牌商名称" width="180">
      </el-table-column>
      <el-table-column prop="picUrl" align="center" label="品牌商图片">
        <template slot-scope="scope">
          <img :src="scope.row.picUrl" width="80">
        </template>
      </el-table-column>
      <el-table-column align="center" label="介绍" prop="desc">
      </el-table-column>
      <el-table-column prop="floorPrice" align="center" label="底价">
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="handleUpdate(scope.row)" type="primary" size="small">编辑</el-button>
          <el-button @click="handleDelete(scope.row)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />
    <el-dialog :title="dialogStatus" :visible.sync="dialogFromVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm"  label-width="100px" size="mini" style="width: 400px;margin-left: 20px;">
        <el-form-item label="品牌方名称" prop="name">
          <el-input v-model="dataForm.name"></el-input>
        </el-form-item>
        <el-form-item label="介绍" prop="desc">
          <el-input v-model="dataForm.desc"></el-input>
        </el-form-item>
        <el-form-item label="品牌商图片" prop="picUrl">
          <el-upload class="avatar-uploader" :action="uploadPath" :headers="headers"
            :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="dataForm.picUrl" :src="dataForm.picUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="底价" prop="floorPrice">
          <el-input v-model="dataForm.floorPrice"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFromVisible = false">取消</el-button>
        <el-button v-if="dialogStatus === '新增'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import Pagination from '@/components/Pagination'
  import {
    listBrand,
    createBrand,
    updateBrand,
    deleteBrand,
    uploadPath
  } from '@/api/malls/brand'
  import {getToken} from '@/utils/auth'
  import Cookies from 'js-cookie'
  export default {
    name: 'Brand',
    components: {
      Pagination
    },
    computed: {
      headers(){
        return {'Shopping-Admin-Token':getToken()}
      }
    },
    data() {
      return {
        uploadPath,
        //查询数据
        listQuery: {
          page: 1,
          limit: 10,
          id: '',
          name: '',
        },
        //总条数
        total: 0,
        //表格数据
        tableData: [],
        //添加或修改模态框状态
        dialogFromVisible: false,
        //模型框类型
        dialogStatus:'新增',
        //新增表单数据
        dataForm: {
          id: '',
          name: '',
          desc: '',
          floorPrice: '',
          picUrl: ''
        },
        //校验规则
        rules: {
          name: [{required: true , message: '品牌商名称不能为空',trigger:'blur'}]
        }
      }
    },
    created() {
      // 数据初始化
      this.getList();
      console.log(Cookies.get('Shopping-Admin-Token'))
    },
    methods: {
      //查询列表
      handleFilter() {
        this.listQuery.page = 1;
        this.getList();
      },
      //获取列表数据接口
      getList() {
        listBrand(this.listQuery).then(res => {
          console.log(res);
          this.tableData = [];
          this.tableData = res.data.data.list;
          this.total = res.data.data.total;
        })
      },
      //重置表单数据
      resetForm(){
        this.dataForm = {
          id: '',
          name: '',
          desc: '',
          foorPrice: '',
          picUrl: ''
        }
      },
      //打开新增模态框
      handleCreate() {
        this.resetForm();
        this.dialogStatus = '新增';
        this.dialogFromVisible = true;
      },
      //确定新增
      createData() {
        this.$refs['dataForm'].validate(valid => {
          if(valid){
            createBrand(this.dataForm).then(res => {
              this.dialogFromVisible = false;
              this.getList();
              this.$notify.success({
                title: '成功',
                message: '新增成功'
              });
            })
          }
        })
      },
      //打开修改模态框
      handleUpdate(row) {
        this.dataForm = Object.assign({}, row);
        this.dialogStatus = '修改';
        this.dialogFromVisible = true;
      },
      //确定修改
      updateData(){
        this.$refs['dataForm'].validate(valid => {
          if(valid){
            updateBrand(this.dataForm).then(()=>{
              this.dialogFromVisible = false;
              this.getList();
              this.$notify.succewss({
                title: '成功',
                message: '修改成功'
              })
            })
          }
        })
      },
      //删除
      handleDelete(row) {
        this.$confirm('此操作将删除该品牌商，是否继续？','提示',{
          confirmButtonText: '确定',
          concelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteBrand(row).then(res => {
            this.getList();
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
          })
        }).catch(() => {
           this.$message({
             type: 'info',
             message: '已取消删除'
           })
        })
      },
      //图片上传成功后执行的方法
      handleAvatarSuccess(res, file) {
        this.dataForm.picUrl = res.data.url
      },
      //图片上传成功前执行的方法
      beforeAvatarUpload(file) {
        // const isJPG = file.type === 'image/jpeg'; 
        // const isLt2M = file.size / 1024 / 1024 < 2;
        // if (!isJPG) {
        //   this.$message.error('上传头像图片只能是 JPG 格式!');
        // }
        // if (!isLt2M) {
        //   this.$message.error('上传头像图片大小不能超过 2MB!');
        // }
        // return isJPG && isLt2M;
      }
    }
  }

</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
<style scoped>
</style>
